from typing import TypeVar

from motor.core import AgnosticCollection
from pydantic import BaseModel

from dao.base_dao import BaseDAO
from db import get_user
from models.user_model import UserModel

T = TypeVar('T', bound=BaseModel)


class UserDAO(BaseDAO[UserModel]):
    def get_db(self) -> AgnosticCollection:
        return get_user()

    async def get_user_by_username(self, username):
        db = self.get_db()
        user = await db.find_one({'username': username})
        return user

    async def get_users_by_query(self, params):
        db = self.get_db()
        query = {}
        if params.get('page') and params.get('pageSize'):
            page = int(params['page']) - 1
            page_size = int(params['pageSize'])
            res = db.find(query).skip(page).limit(page_size)
        else:
            res = db.find(query)
        users = [i async for i in res]
        return users
